package com;

import com.alibaba.fastjson.JSON;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharacterRecognition {

    static final String DSR = "当事人";

    static final String JCM = "经查明";

    static final Map<String, String> KEY_INFO = (Map<String, String>) JSON.parse("{\n" +
            "    \"DSR\":\"当事人\",\n" +
            "    \"ZTZGZZMC\":\"主体资格证照名称\",\n" +
            "    \"TYSHXYDM\":\"统一社会信用代码\",\n" +
            "    \"ZS\":\"住所\",\n" +
            "    \"FDDBR\":\"法定代表人（负责人、经营者）\",\n" +
            "    \"SFZHM\":\"身份证号码\",\n" +
            "    \"JYZ\":\"经营者\",\n" +
            "    \"LXDH\":\"联系电话\",\n" +
            "    \"LXDZ\":\"联系地址\"\n" +
            "}");

    static final String WRAP = "\n";

    static final Pattern PATTERN_CHINESE = Pattern.compile("[\u4e00-\u9fa5]+");
    static final Pattern PATTERN_NON_CHINESE = Pattern.compile("(![\u4e00-\u9fa5])+");
    static final Pattern PATTERN_NUMBER = Pattern.compile("[0-9]+");

    public static void main(String[] args) throws Exception {
        System.out.println(CharacterRecognition.analysis(Words.ORIGIN_WORD16));
    }
    public static String analysis(String QW) throws Exception{
        String WS = QW.trim();
        if (WS.indexOf(CharacterRecognition.DSR) >= 0) {
            WS = WS.substring(0, WS.indexOf(CharacterRecognition.DSR));
        }
        if (getStringNum(WS,CharacterRecognition.WRAP, 3) >= 0) {
            WS = WS.substring(0, getStringNum(WS,CharacterRecognition.WRAP, 3));
        }
        String WSZZDW = WS.substring(0, WS.indexOf(CharacterRecognition.WRAP));
        String WSMC = WS.substring(WS.indexOf(CharacterRecognition.WRAP), getStringNum(WS,CharacterRecognition.WRAP, 2)).trim();
        String AH = WS.substring(getStringNum(WS,CharacterRecognition.WRAP, 2), getStringNum(WS,CharacterRecognition.WRAP, 3)).trim();

        Matcher matcher = CharacterRecognition.PATTERN_CHINESE.matcher(AH);
        matcher.find();
        String ZH = matcher.group(0);
        Matcher matcher1 = CharacterRecognition.PATTERN_NUMBER.matcher(AH);
        matcher1.find();
        String LAND = matcher1.group();
        matcher1.find();
        String SXH = matcher1.group();

        String[] split = QW.replace(WS, "").split(CharacterRecognition.WRAP);
        int i = 0;
        StringBuffer sb = new StringBuffer();
        List<Element> infos = new ArrayList();
        while(split[i].length() < 60){
            Element elementFromString = getElementFromString(split[i]);
            if (elementFromString != null){
                infos.add(elementFromString);
            }
            sb.append(split[i++]).append(CharacterRecognition.WRAP);
        }
        String DSRXX = sb.toString();

        String WWDW = split[split.length - 3];
        String WWRQ = split[split.length - 2];
        String WWBZ = split[split.length - 1];
        String WW = WWDW + CharacterRecognition.WRAP + WWRQ + CharacterRecognition.WRAP + WWBZ;

        String ZW = QW.replace(WS, "").replace(DSRXX, "").replace(WW, "");

        String AJLY = "";
        String WFFL = "";
        if (ZW.indexOf(JCM) >= 0){
            AJLY = ZW.split(JCM)[0];
            WFFL = ZW.split(JCM)[1];
        }




        Element root = new DOMElement("writ");
        Element qwElement = CharacterRecognition.buildElement("QW", "全文", QW);
        Element wsElement = CharacterRecognition.buildElement("WS", "文首", WS);

        Element ahElement = CharacterRecognition.buildElement("AH", "案号", AH);
        ahElement.add(CharacterRecognition.buildElement("ZH", "字号", ZH));
        ahElement.add(CharacterRecognition.buildElement("LAND", "立案年度", LAND));
        ahElement.add(CharacterRecognition.buildElement("AHSXH", "案号顺序号", SXH));

        wsElement.add(CharacterRecognition.buildElement("WSZZDW", "文书制作单位", WSZZDW));
        wsElement.add(CharacterRecognition.buildElement("WSMC", "文书名称", WSMC));
        wsElement.add(ahElement);

        Element dsrxxElement = CharacterRecognition.buildElement("DSRXX", "当事人信息", DSRXX);
        for (Element info : infos) {
            dsrxxElement.add(info);
        }

        Element zwElement = CharacterRecognition.buildElement("ZW", "正文", ZW);
        zwElement.add(CharacterRecognition.buildElement("AJLY", "案件来源", AJLY));
        zwElement.add(CharacterRecognition.buildElement("WFFL", "违反法律", WFFL));


        Element wwElement = CharacterRecognition.buildElement("WW", "文尾", WW);
        wwElement.add(CharacterRecognition.buildElement("WWDW", "文书单位", WWDW));
        wwElement.add(CharacterRecognition.buildElement("WWRQ", "文书日期", WWRQ));
        wwElement.add(CharacterRecognition.buildElement("WWBZ", "文书备注", WWBZ));

        qwElement.add(wsElement);
        qwElement.add(dsrxxElement);
        qwElement.add(zwElement);
        qwElement.add(wwElement);

        root.add(qwElement);
        Document document = DocumentHelper.createDocument(root);

        return document.asXML();
    }

    public static Element getElementFromString(String origin){
        for(Map.Entry<String, String> entry : KEY_INFO.entrySet()){
            if (origin.indexOf(entry.getValue()) >= 0){
                String v = "";
                String[] split = origin.split("：");
                if (split.length > 1){
                    v = Optional.of(split[1]).orElse("").trim();
                }
                return CharacterRecognition.buildElement(entry.getKey(), entry.getValue(), v);
            }
        }
        return null;
    }

    public static Element buildElement(String nodeName, String cnName, String value){
        return new DOMElement(nodeName).addAttribute("nameCN", cnName).addAttribute("value", value);
    }

    public static int getStringNum(String sA,String sB,int num){
        int weizhi = 0;
        for (int i = 1; i < num; i++) {
            if (weizhi == 0) {
                weizhi = sA.toString().indexOf(sB);
            }
            weizhi = sA.indexOf(sB, weizhi+1);
        }
        return weizhi+1;
    }
}
